package com.ning.weixin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

import com.ning.weixin.entity.AcMember;

/**
 * @author MENG_LI_DONG
 * @version 2018年12月18日 下午9:55:43
 */
@Component
public class AcMemberDao {
	private Logger log = LoggerFactory.getLogger(AcMemberDao.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	public Long insertReturnId(AcMember t){
		final StringBuffer sql = new StringBuffer();
		StringBuffer sqlParams = new StringBuffer();
		final List<Object> params = new ArrayList<>();
		sql.append("insert into ac_member(create_time,update_time,is_deleted,");
		sqlParams.append("?,?,?,");
		params.add(new Date());
		params.add(params.get(0));
		params.add(0);
		if(t.getUsername()!=null){
			sql.append("username,");
			sqlParams.append("?,");
			params.add(t.getUsername());
		}
		if(t.getRealname()!=null){
			sql.append("realname,");
			sqlParams.append("?,");
			params.add(t.getRealname());
		}
		if(t.getPassword()!=null){
			sql.append("password,");
			sqlParams.append("?,");
			params.add(t.getPassword());
		}
		if(t.getNickname()!=null){
			sql.append("nickname,");
			sqlParams.append("?,");
			params.add(t.getNickname());
		}
		if(t.getPhone()!=null){
			sql.append("phone,");
			sqlParams.append("?,");
			params.add(t.getPhone());
		}
		if(t.getMobile()!=null){
			sql.append("mobile,");
			sqlParams.append("?,");
			params.add(t.getMobile());
		}
		if(t.getEmail()!=null){
			sql.append("email,");
			sqlParams.append("?,");
			params.add(t.getEmail());
		}
		if(t.getAvatar()!=null){
			sql.append("avatar,");
			sqlParams.append("?,");
			params.add(t.getAvatar());
		}
		if(t.getOpenid()!=null){
			sql.append("openid,");
			sqlParams.append("?,");
			params.add(t.getOpenid());
		}
		sql.deleteCharAt(sql.length()-1);
		sqlParams.deleteCharAt(sqlParams.length()-1);
		sql.append(") values(").append(sqlParams).append(")");
		log.debug("sql:"+sql);
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbcTemplate.update(new PreparedStatementCreator() {
			
			@Override
			public PreparedStatement createPreparedStatement(Connection conn)
					throws SQLException {
				PreparedStatement ps = conn.prepareStatement(sql.toString(),Statement.RETURN_GENERATED_KEYS);
				int index = 1;
				for(Object obj:params){
					ps.setObject(index, obj);
					index++;
				}
				return ps;
			}
		},keyHolder);
		return keyHolder.getKey().longValue();
	}
	public void updateRealnameAndPhoneByOpenid(String openid,String realname,String phone){
		String sql = "update ac_member set realname=?, phone=? where openid=?";
		jdbcTemplate.update(sql, realname,phone,openid);
	}
}
